<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Fetching Options and Arguments - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.console.getopt.fetching.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.console.getopt.fetching.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.console.getopt.rules.html">Declaring Getopt Rules</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.console.getopt.html">Zend_Console_Getopt</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.console.getopt.configuration.html">Configuring Zend_Console_Getopt</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.console.getopt.fetching" class="section"><div class="info"><h1 class="title">Fetching Options and Arguments</h1></div>
    

    <p class="para">
        After you have declared the options that the
        <span class="classname">Zend_Console_Getopt</span> object should recognize, and supply
        arguments from the command-line or an array, you can
        query the object to find out which options were specified by a user in
        a given command-line invocation of your program. The class implements
        magic methods so you can query for options by name.
    </p>

    <p class="para">
        The parsing of the data is deferred until the first query you make
        against the <span class="classname">Zend_Console_Getopt</span> object to find out if an
        option was given, the object performs its parsing. This allows you to
        use several method calls to configure the options, arguments, help
        strings, and configuration options before parsing takes place.
    </p>

    <div class="section" id="zend.console.getopt.fetching.exceptions"><div class="info"><h1 class="title">Handling Getopt Exceptions</h1></div>
        

        <p class="para">
            If the user gave any invalid options on the command-line,
            the parsing function throws a <span class="classname">Zend_Console_Getopt_Exception</span>.
            You should catch this exception in your application code.
            You can use the  <span class="methodname">parse()</span> method to force the object
            to parse the arguments. This is useful because you can invoke
             <span class="methodname">parse()</span> in a <em class="emphasis">try</em> block. If it passes,
            you can be sure that the parsing won&#039;t throw an exception again.
            The exception thrown has a custom method  <span class="methodname">getUsageMessage()</span>,
            which returns as a string the formatted set of usage messages for
            all declared options.
        </p>

        <div class="example" id="zend.console.getopt.fetching.exceptions.example"><div class="info"><p><b>Example #1 Catching Getopt Exceptions</b></p></div>
            

            <pre class="programlisting brush: php">
try {
    $opts = new Zend_Console_Getopt(&#039;abp:&#039;);
    $opts-&gt;parse();
} catch (Zend_Console_Getopt_Exception $e) {
    echo $e-&gt;getUsageMessage();
    exit;
}
</pre>

        </div>

        <p class="para">
            Cases where parsing throws an exception include:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    Option given is not recognized.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    Option requires a parameter but none was given.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    Option parameter is of the wrong type.
                    E.g. a non-numeric string when an integer
                    was required.
                </p>
            </li>
        </ul>
    </div>

    <div class="section" id="zend.console.getopt.fetching.byname"><div class="info"><h1 class="title">Fetching Options by Name</h1></div>
        

        <p class="para">
            You can use the  <span class="methodname">getOption()</span> method to query the value
            of an option. If the option had a parameter, this method returns
            the value of the parameter. If the option had no parameter but
            the user did specify it on the command-line, the method returns
            <b><tt>TRUE</tt></b>. Otherwise the method returns <b><tt>NULL</tt></b>.
        </p>

        <div class="example" id="zend.console.getopt.fetching.byname.example.setoption"><div class="info"><p><b>Example #2 Using getOption()</b></p></div>
            

            <pre class="programlisting brush: php">
$opts = new Zend_Console_Getopt(&#039;abp:&#039;);
$b = $opts-&gt;getOption(&#039;b&#039;);
$p_parameter = $opts-&gt;getOption(&#039;p&#039;);
</pre>

        </div>

        <p class="para">
            Alternatively, you can use the magic  <span class="methodname">__get()</span> function
            to retrieve the value of an option as if it were a class member
            variable. The  <span class="methodname">__isset()</span> magic method is also
            implemented.
        </p>

        <div class="example" id="zend.console.getopt.fetching.byname.example.magic"><div class="info"><p><b>Example #3 Using __get() and __isset() Magic Methods</b></p></div>
            

            <pre class="programlisting brush: php">
$opts = new Zend_Console_Getopt(&#039;abp:&#039;);
if (isset($opts-&gt;b)) {
    echo &quot;I got the b option.\n&quot;;
}
$p_parameter = $opts-&gt;p; // null if not set
</pre>

        </div>

        <p class="para">
            If your options are declared with aliases, you may use any of the
            aliases for an option in the methods above.
        </p>
    </div>

    <div class="section" id="zend.console.getopt.fetching.reporting"><div class="info"><h1 class="title">Reporting Options</h1></div>
        

        <p class="para">
            There are several methods to report the full set of
            options given by the user on the current command-line.
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    As a string: use the  <span class="methodname">toString()</span> method. The options
                    are returned as a space-separated string of <strong class="command">flag=value</strong>
                    pairs. The value of an option that does not have a parameter
                    is the literal string &quot;<b><tt>TRUE</tt></b>&quot;.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    As an array: use the  <span class="methodname">toArray()</span> method. The options
                    are returned in a simple integer-indexed array of strings, the flag
                    strings followed by parameter strings, if any.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    As a string containing <acronym class="acronym">JSON</acronym> data: use the
                     <span class="methodname">toJson()</span> method.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    As a string containing <acronym class="acronym">XML</acronym> data: use the
                     <span class="methodname">toXml()</span> method.
                </p>
            </li>
        </ul>

        <p class="para">
            In all of the above dumping methods, the flag string is the
            first string in the corresponding list of aliases. For example,
            if the option aliases were declared like <strong class="command">verbose|v</strong>,
            then the first string, <strong class="command">verbose</strong>, is used as the
            canonical name of the option. The name of the option flag does not
            include any preceding dashes.
        </p>
    </div>

    <div class="section" id="zend.console.getopt.fetching.remainingargs"><div class="info"><h1 class="title">Fetching Non-option Arguments</h1></div>
        

        <p class="para">
            After option arguments and their parameters have been
            parsed from the command-line, there may be additional arguments
            remaining. You can query these arguments using the
             <span class="methodname">getRemainingArgs()</span> method. This method returns
            an array of the strings that were not part of any options.
        </p>

        <div class="example" id="zend.console.getopt.fetching.remainingargs.example"><div class="info"><p><b>Example #4 Using getRemainingArgs()</b></p></div>
            

            <pre class="programlisting brush: php">
$opts = new Zend_Console_Getopt(&#039;abp:&#039;);
$opts-&gt;setArguments(array(&#039;-p&#039;, &#039;p_parameter&#039;, &#039;filename&#039;));
$args = $opts-&gt;getRemainingArgs(); // returns array(&#039;filename&#039;)
</pre>

        </div>

        <p class="para">
            <span class="classname">Zend_Console_Getopt</span> supports the <acronym class="acronym">GNU</acronym>
            convention that an argument consisting of a double-dash signifies the end of
            options. Any arguments following this signifier must be treated as
            non-option arguments. This is useful if you might have a non-option
            argument that begins with a dash.
            For example: &quot;<strong class="command">rm -- -filename-with-dash</strong>&quot;.
        </p>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.console.getopt.rules.html">Declaring Getopt Rules</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.console.getopt.html">Zend_Console_Getopt</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.console.getopt.configuration.html">Configuring Zend_Console_Getopt</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.console.getopt.html">Zend_Console_Getopt</a></li>
  <li><a href="zend.console.getopt.introduction.html">Introduction</a></li>
  <li><a href="zend.console.getopt.rules.html">Declaring Getopt Rules</a></li>
  <li class="active"><a href="zend.console.getopt.fetching.html">Fetching Options and Arguments</a></li>
  <li><a href="zend.console.getopt.configuration.html">Configuring Zend_Console_Getopt</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>